﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AG.Editor.Core.Data;
using AG.Editor.Core;
using AG.Editor.Core.Metadata;

namespace AG.Editor.ModelUI.Windows
{
    public partial class AGEditModelSummaryWindow : Form
    {
        private AGModel _model;

        public AGEditModelSummaryWindow(AGModel model)
        {
            InitializeComponent();

            _model = model;

            _ctlEditId.Text = _model.Id.ToString();
            _ctlEditCaption.Text = _model.Caption;

            for (int index = 0; index < AGEContext.Current.EProject.TProject.ModelCategories.Count; index++)
            {
                AGModelCategory category = AGEContext.Current.EProject.TProject.ModelCategories[index];
                if (ValidateModelCategory(_model, category))
                {
                    _ctlListCategory.Items.Add(category);
                }
            }

            for (int index = 0; index < _ctlListCategory.Items.Count; index++)
            {
                AGModelCategory category = _ctlListCategory.Items[index] as AGModelCategory;
                if (model.CategoryId == category.Id)
                {
                    _ctlListCategory.SelectedIndex = index;
                }
            }
        }

        private void _ctlBtnOK_Click(object sender, EventArgs e)
        {
            int modelId = _model.Id;
            string modelCaption = _model.Caption;
            AGModelCategory modelCategory = _ctlListCategory.SelectedItem as AGModelCategory;
            if (modelCategory == null)
            {
                return;
            }

            try
            {
                modelId = Convert.ToInt32(_ctlEditId.Text);
                modelCaption = _ctlEditCaption.Text;
            }
            catch
            {
                return;
            }

            _model.Id = modelId;
            _model.Caption = modelCaption;
            _model.CategoryId = modelCategory.Id;
            _model.Category = modelCategory;
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private bool ValidateModelCategory(AGModel model, AGModelCategory modelCategory)
        {
            if (model.Actions.Count == modelCategory.Actions.Count)
            {
                if (model.Actions[0].Directions.Count == modelCategory.Actions[0].Directions.Count)
                {
                    return true;
                }
            }
            return false;
        }
    }
}
